/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package User;

import Bean.myPhotoBeanRemote;
import Bean.userBeanRemote;
import Objects.Album;
import Objects.Comment;
import Objects.Image;
import Objects.User;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 *
 * @author tinvukhac
 */
public class MyPhotoController extends MultiActionController {

    InitialContext context;

    public ModelAndView viewAlbum(HttpServletRequest request,
            HttpServletResponse response) throws NamingException {
        ModelAndView view = new ModelAndView("viewAlbum");

        try {
            context = new InitialContext();
            myPhotoBeanRemote myPhotoBean = (myPhotoBeanRemote) context.lookup("myPhotoBean");
            String albumId = request.getParameter("albumId");
            List<Image> imageList = myPhotoBean.getImageListByAlbumId(albumId);
            view.addObject("imageList", imageList);
            String albumName=myPhotoBean.getAlbumNameByAlbumId(albumId);
            view.addObject("albumName", albumName);
            
            HttpSession session=request.getSession();
            String userName=(String)session.getAttribute("userName");
            view.addObject("userName", userName);
            int userId=(Integer)session.getAttribute("userId");
            int numberOfImagesInAlbum=myPhotoBean.getNumberOfImagesInAlbum(Integer.parseInt(albumId), userId);
            view.addObject("numberOfImagesInAlbum", numberOfImagesInAlbum);
            
            userBeanRemote userBean=(userBeanRemote)context.lookup("userBean");           
            User user=userBean.getUserInfoByUserId(userId);
            int profileAlbumId=user.getProfileAlbumId();
            String thumbnailProfileFileName = userBean.getFileNameThumbnailOfAlbums(profileAlbumId);
            view.addObject("thumbnailProfileFileName", thumbnailProfileFileName);
            Album album=myPhotoBean.getAlbumFromAlbumId(Integer.parseInt(albumId));
            view.addObject("album", album);
            
        } catch (Exception e) {
        }

        return view;
    }

    public ModelAndView viewImage(HttpServletRequest request,
            HttpServletResponse response) {
        ModelAndView view = new ModelAndView("viewImage");
        String albumId = request.getParameter("albumId");
        int imageIndex = Integer.parseInt(request.getParameter("imageIndex"));
        try {
            context = new InitialContext();
            userBeanRemote userBean = (userBeanRemote) context.lookup("userBean");
            myPhotoBeanRemote myPhotoBean = (myPhotoBeanRemote) context.lookup("myPhotoBean");

            HttpSession session = request.getSession();
            int userId = userBean.getUserIdByUserName(session.getAttribute("userName").toString());
            User currentUser = userBean.getUserInfoByUserId(userId);

            List<Image> imageList = myPhotoBean.getImageListByAlbumId(albumId);
            String albumName = myPhotoBean.getAlbumNameByAlbumId(albumId);
            Image currentImage = (Image) imageList.get(imageIndex);
            List<Comment> commentList = myPhotoBean.getCommentListByImageId(Integer.toString(currentImage.getImageId()));
            for (int i = 0; i < commentList.size(); i++) {
                Comment tempComment = (Comment) commentList.get(i);
                User commentUser = userBean.getUserInfoByUserId(tempComment.getUserId());
                tempComment.setUser(commentUser);
                tempComment.setIsDislikeByCurrentUser(myPhotoBean.checkCommentWasDislikeByUser(Integer.toString(tempComment.getCommentId()), Integer.toString(currentUser.getUserId())));
            }
            view.addObject("currentUser", currentUser);
            view.addObject("imageList", imageList);
            view.addObject("imageIndex", imageIndex);
            view.addObject("albumId", albumId);
            view.addObject("albumName", albumName);
            view.addObject("commentList", commentList);
            
            String userName=(String)session.getAttribute("userName");
            view.addObject("userName", userName);
            int profileAlbumId=currentUser.getProfileAlbumId();
            String thumbnailProfileFileName = userBean.getFileNameThumbnailOfAlbums(profileAlbumId);
            view.addObject("thumbnailProfileFileName", thumbnailProfileFileName);
        } catch (Exception e) {
        }

        return view;
    }

    public ModelAndView myPhoto(HttpServletRequest request,
            HttpServletResponse response) {
        ModelAndView view = new ModelAndView("myPhoto");

        // Get Album of user
        try {
            context = new InitialContext();
            userBeanRemote userBean = (userBeanRemote) context.lookup("userBean");
            HttpSession session = request.getSession();
            String userName = session.getAttribute("userName").toString();
            Boolean isAssmin = userBean.checkIsAssmin(userName);
            if (isAssmin) {
                response.sendRedirect(request.getContextPath() + "/admin/admin.htm");
            }
            String expirationDateStr = userBean.checkIsBlock(userName);
            if (expirationDateStr.compareTo("ActiveUser") != 0) {
                response.sendRedirect(request.getContextPath() + "/login/block.htm?expirationDateStr=" + expirationDateStr);
            }
            myPhotoBeanRemote myPhotoBean = (myPhotoBeanRemote) context.lookup("myPhotoBean");
            int userId = userBean.getUserIdByUserName(userName);
            List<Album> albumList = myPhotoBean.getAlbumListByUserId(userId);
            view.addObject("albumList", albumList);
            User user=userBean.getUserInfoByUserId((Integer)session.getAttribute("userId"));
            int profileAlbumId=user.getProfileAlbumId();
            String thumbnailFileName = userBean.getFileNameThumbnailOfAlbums(profileAlbumId);
            view.addObject("thumbnailFileName", thumbnailFileName);
            view.addObject("userName", userName);
            int numberOfAlbums=userBean.getNumberOfAlbums(userId);
            view.addObject("numberOfAlbums", numberOfAlbums);
            int numberOfImages=userBean.getNumberOfImages(userId);
            view.addObject("numberOfImages", numberOfImages);
      
        } catch (Exception e) {
        }

        return view;
    }

    public void postComment(HttpServletRequest request,
            HttpServletResponse response) {
        String commentContent = request.getParameter("comment");
        String imageId = request.getParameter("imageId");
        String userId = request.getParameter("userId");
        String albumId = request.getParameter("albumId");
        String imageIndex = request.getParameter("imageIndex");
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date today = Calendar.getInstance().getTime();
        String commentDatetime = df.format(today);
        try {
            context = new InitialContext();
            myPhotoBeanRemote myPhotoBean = (myPhotoBeanRemote) context.lookup("myPhotoBean");
            myPhotoBean.insertNewComment(imageId, userId, commentContent, commentDatetime);
            response.sendRedirect(request.getContextPath()
                    + "/myphoto/viewImage.htm?albumId="
                    + albumId + "&imageIndex=" + imageIndex);
        } catch (Exception e) {
        }
    }

    public void reportComment(HttpServletRequest request,
            HttpServletResponse response) {
        String commentId = request.getParameter("commentId");
        String userId = request.getParameter("userId");
        String albumId = request.getParameter("albumId");
        String imageIndex = request.getParameter("imageIndex");

        try {
            context = new InitialContext();
            myPhotoBeanRemote myPhotoBean = (myPhotoBeanRemote) context.lookup("myPhotoBean");
            myPhotoBean.insertNewReport(commentId, userId);
            response.sendRedirect(request.getContextPath()
                    + "/myphoto/viewImage.htm?albumId="
                    + albumId + "&imageIndex=" + imageIndex);
        } catch (Exception e) {
        }
    }
}
